from task import Task
from job import Job
from simu_ann_mid import *
# from TaskSet_4_9_9 import sets
# from TaskSetMoti_n24 import sets
import threading
import sys
import os


path='/Users/liuchangkundeimac/real-time-scheduling/generator'
sys.path
sys.path.append(path)
import csv
import generator.task_generator as g
import math

parameters = []
with open('/Users/liuchangkundeimac/real-time-scheduling/parameters.csv', encoding="utf-8") as csvfile:
    csv_reader = csv.reader(csvfile) 
    birth_header = next(csv_reader) 
    for row in csv_reader:  
        parameters.append(row)

def GeneTaskSets(file_name,parameters):
    path = file_name + 'tesk_set.log'
    f = open(path,'w')
    print(parameters,file=f)
    n = int(parameters[0][0])
    nsets = int(parameters[0][1])
    u_c = float(parameters[0][2])
    Usets = g.UUniFastDiscard(n,u_c,nsets)
    c_set = g.Computing_cost(n)
    Psets = g.Period(Usets,c_set)
    Dsets = g.Deadline(c_set,Psets)
    Rsets = g.Upload_cost(c_set,Dsets)
    print("utilization:",Usets,file=f)
    print("computing_cost:",c_set,file=f)
    print("upload_cost:",Rsets,file=f)
    print("Deadline:",Dsets,file=f)
    print("Period:",Psets,file=f)
    TSets = []
    for i in range(nsets):
        task_set = []
        for j in range(n):
            task_set.append(Task(c_set[j],Rsets[i][j],0,Dsets[i][j],Psets[i][j]))
        TSets.append(task_set)
    task_set = TSets[0]
    u = Channel_uti(task_set)
    zero_flag = 0
    while( (u>=0.91 or u<= 0.9) or zero_flag == 1 or min(Usets[0])<0.1):
        print("channel utilization:",u)
        Usets = g.UUniFastDiscard(n,u_c,nsets)
        c_set = g.Computing_cost(n)
        Psets = g.Period(Usets,c_set)
        Dsets = g.Deadline(c_set,Psets)
        Rsets = g.Upload_cost(c_set,Dsets)
        print("computation utilization :",Usets,file=f)
        print("computing_cost:",c_set,file=f)
        print("upload_cost:",Rsets,file=f)
        print("Deadline:",Dsets,file=f)
        print("Period:",Psets,file=f)
        zero_flag = 0
        TSets = []
        for i in range(nsets):
            task_set = []
            for j in range(n):
                task_set.append(Task(c_set[j],Rsets[i][j],0,Dsets[i][j],Psets[i][j]))
            TSets.append(task_set)
        task_set = TSets[0]
        for i in range(len(task_set)):
            t_i = task_set[i]
            c_i = t_i.getComcost()
            r_i = t_i.getUpcost()
            if c_i==0 or r_i==0:
                zero_flag = 1 
        u = Channel_uti(task_set)
    return task_set

def GetMidSet(exp):
    file_name = f'/Users/liuchangkundeimac/real-time-scheduling/FeasibleTest/4_9_50/test{exp}/'
    if not os.path.isfile(file_name):
        os.makedirs(file_name)
        task_set = GeneTaskSets(file_name,parameters)
        mddl_set,until = SimuAnnMid(task_set,file_name)
    else:
        task_set = GeneTaskSets(file_name,parameters)
        mddl_set,until = SimuAnnMid(task_set,file_name)

if __name__ == '__main__':
    for i in range(1):
        t = threading.Thread(target=GetMidSet,args=(i,))
        t.start()